package com.ruoyi.system.service.project.impl;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.project.research.ResearchProjects;
import com.ruoyi.system.domain.project.research.ResearchProjectsMember;
import com.ruoyi.system.mapper.project.ResearchProjectsMapper;
import com.ruoyi.system.mapper.project.ResearchProjectsMemberMapper;
import com.ruoyi.system.service.project.IResearchProjectsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.ruoyi.common.utils.SecurityUtils.getUserId;

/**
 * 科研项目情况Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-04
 */
@Service
public class ResearchProjectsServiceImpl implements IResearchProjectsService
{
    @Autowired
    private ResearchProjectsMapper researchProjectsMapper;
    @Autowired
    private ResearchProjectsMemberMapper researchProjectsMemberMapper;
    /**
     * 查询科研项目情况
     * 
     * @param id 科研项目情况主键
     * @return 科研项目情况
     */
    @Override
    public ResearchProjects selectResearchProjectsById(Long id)
    {
        ResearchProjects researchProjects =researchProjectsMapper.selectResearchProjectsById(id);
        ResearchProjectsMember researchProjectsMember = new ResearchProjectsMember();
        researchProjectsMember.setClassId(id);
        List<ResearchProjectsMember> researchProjectsMembers = researchProjectsMemberMapper.selectResearchProjectsMemberList(researchProjectsMember);
        researchProjects.setTeamMemberList(researchProjectsMembers);
        return  researchProjects;
    }

    /**
     * 查询科研项目情况列表
     * 
     * @param researchProjects 科研项目情况
     * @return 科研项目情况
     */
    @Override
    public List<ResearchProjects> selectResearchProjectsList(ResearchProjects researchProjects)
    {
        //如果是老师，则只显示自己上传的
        if(researchProjects.getCurrentRole()!=null&&researchProjects.getCurrentRole().equals("teacher")){
            researchProjects.setTeacherId(String.valueOf(getUserId()));
        }
        //如果是部门，则显示老师是系里的所有
        if(researchProjects.getCurrentRole()!=null&&researchProjects.getCurrentRole().equals("department")){
            researchProjects.setDeptId(SecurityUtils.getDeptId());
        }
        return researchProjectsMapper.selectResearchProjectsList(researchProjects);
    }

    /**
     * 新增科研项目情况
     * 
     * @param researchProjects 科研项目情况
     * @return 结果
     */
    @Override
    public int insertResearchProjects(ResearchProjects researchProjects)
    {
        if(researchProjects.getId()!=null){
            ResearchProjectsMember researchProjectsMember = new ResearchProjectsMember();
            researchProjectsMember.setClassId(researchProjects.getId());
            researchProjectsMember.setMemberId(Long.valueOf(researchProjects.getTeacherId()));
            researchProjectsMember.setMemberName(researchProjects.getTeacherName());
            researchProjectsMemberMapper.insertResearchProjectsMember(researchProjectsMember);
        }else {
            int result =researchProjectsMapper.insertResearchProjects(researchProjects);
            if(result>0){
                ResearchProjectsMember researchProjectsMember = new ResearchProjectsMember();
                researchProjectsMember.setClassId(researchProjects.getId());
                researchProjectsMember.setMemberId(Long.valueOf(researchProjects.getTeacherId()));
                researchProjectsMember.setMemberName(researchProjects.getTeacherName());
                researchProjectsMemberMapper.insertResearchProjectsMember(researchProjectsMember);
            }
        }
        return 1;
    }

    /**
     * 修改科研项目情况
     * 
     * @param researchProjects 科研项目情况
     * @return 结果
     */
    @Override
    public int updateResearchProjects(ResearchProjects researchProjects)
    {
        return researchProjectsMapper.updateResearchProjects(researchProjects);
    }

    /**
     * 批量删除科研项目情况
     * 
     * @param ids 需要删除的科研项目情况主键
     * @return 结果
     */
    @Override
    public int deleteResearchProjectsByIds(Long[] ids)
    {
        for (Long id : ids){
            ResearchProjectsMember researchProjectsMember = new ResearchProjectsMember();
            researchProjectsMember.setClassId(id);
            List<ResearchProjectsMember> researchProjectsMembers = researchProjectsMemberMapper.selectResearchProjectsMemberList(researchProjectsMember);
            for (ResearchProjectsMember researchProjectsMember1 : researchProjectsMembers) {
                researchProjectsMemberMapper.deleteResearchProjectsMemberById(researchProjectsMember1.getId());
            }
        }
        return researchProjectsMapper.deleteResearchProjectsByIds(ids);
    }

    /**
     * 删除科研项目情况信息
     * 
     * @param id 科研项目情况主键
     * @return 结果
     */
    @Override
    public int deleteResearchProjectsById(Long id)
    {
        return researchProjectsMapper.deleteResearchProjectsById(id);
    }
    /**
     * 根据标题查询
     * @param data
     * @return
     */
    @Override
    public ResearchProjects getInfoByTitle(String data) {
        return researchProjectsMapper.getInfoByTitle(data);
    }
}
